package com.thinkgem.jeesite.frame.pm.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thinkgem.jeesite.frame.pm.dao.PmParamMapper;
import com.thinkgem.jeesite.frame.pm.entity.PmParam;
import com.thinkgem.jeesite.frame.pm.utils.PmCacheUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 参数Service
 *
 * @author kongxiangsheng
 * @version 2018-09-03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PmParamService extends ServiceImpl<PmParamMapper, PmParam> implements IService<PmParam> {

    /**
     * 根据code获取参数信息
     *
     * @param paramCode
     * @return
     */
    public PmParam getByCode(String paramCode) {
        // 从缓存中获取所有的数据字典
        PmParam cachePmParam = PmCacheUtils.getPmParam(paramCode);
        if (null != cachePmParam && StringUtils.isNotBlank(cachePmParam.getName())) {
            return cachePmParam;
        }

        // 如果获取不到，则从数据库查询
        QueryWrapper queryWrapper = new QueryWrapper<PmParam>();
        queryWrapper.eq(PmParam.COL_CODE, paramCode);
        List<PmParam> pmParamList = this.baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(pmParamList)) {
            PmCacheUtils.putPmParam(paramCode, pmParamList.get(0));
            return pmParamList.get(0);
        }

        // 数据库里也取不到，则返回空List
        return new PmParam();
    }



}